//
//  TXLocationSCViewController.m
//  ShangHaiHuDongApp
//
//  Created by apple on 17/9/11.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "TXLocationSCViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>

@interface TXLocationSCViewController ()<UIWebViewDelegate>
//定位微调webview
@property (nonatomic , strong) UIWebView * locationSCWebView;
//选中的定位信息
@property (nonatomic , strong) NSDictionary * selectedDic;
@end

@implementation TXLocationSCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titlelabel.text = @"位置";
    self.selectedDic = @{};
    [self ybInitUserInterface];
}

-(void)ybInitUserInterface{
    [self.view addSubview:self.locationSCWebView];
    self.leftButton.hidden = NO;
    [self.leftButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [self.leftButton setTitle:@"取消" forState:UIControlStateNormal];
    self.rightButton.hidden = NO;
    [self.rightButton setTitle:@"确定" forState:UIControlStateNormal];
}

-(void)action_rightButton:(UIButton *)sender
{
    [super action_rightButton:sender];
    if (self.selectedDic.count == 0) {
        [YJProgressHUD showSuccess:@"请选择位置" inview:self.view];
        return;
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectLocationPointWithLocationDictionary:)]) {
        [self.delegate didSelectLocationPointWithLocationDictionary:self.selectedDic];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    JSContext * jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    __weak typeof(self) weakself = self;
    jsContext[@"BackCurrentPoint"] = ^(){
        NSArray * args = [JSContext currentArguments];
        NSDictionary * locationDic = [ToolClass getDictionaryFromJsonString:[NSString stringWithFormat:@"%@" , args[0]]];
        if (locationDic.count > 0) {
            weakself.selectedDic = locationDic;
        }
    };
    jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
        context.exception = exceptionValue;
        NSLog(@"异常信息：%@", exceptionValue);
    };
}

-(UIWebView *)locationSCWebView
{
    if (!_locationSCWebView) {
        _locationSCWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - LP_TopHeight)];
        //_aboutUsWebView.scalesPageToFit = YES;
        _locationSCWebView.delegate = self;
        
        NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/WeChat/views/ios_map.html",HTMLURL]];;
        NSURLRequest * request = [NSURLRequest requestWithURL:url];
        [_locationSCWebView loadRequest:request];
    }
    return _locationSCWebView;
}

@end
